package com.zkjl.protect_business.controller;

import com.zkjl.protect_business.common.ApiResponse;
import com.zkjl.protect_business.common.SystemLog;
import com.zkjl.protect_business.dto.PageBaseDTO;
import com.zkjl.protect_business.dto.PageDTO;
import com.zkjl.protect_business.dto.SortDTO;
import com.zkjl.protect_business.dto.TemplateDTO;
import com.zkjl.protect_business.entity.Template;
import com.zkjl.protect_business.service.TemplateService;
import com.zkjl.protect_business.utils.ResultUtil;
import com.zkjl.protect_business.utils.WordUtil;
import com.zkjl.protect_business.vo.NoticeVO;
import com.zkjl.protect_business.vo.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/business-types")
@Api(tags = {"模板"})
public class TemplateController {

    @Autowired
    TemplateService templateService;

    @ApiOperation(value = "上传模板", notes = "上传模板", httpMethod = "POST")
    @PostMapping(value = "{businessId}/templates/upload")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "businessId", value = "业务分类id", required = true, paramType = "path")
    })
    @SystemLog(description = "上传模板")
    public ApiResponse<Template> upload(@RequestParam(value = "file") MultipartFile file,@PathVariable String businessId) throws IOException {

        Template upload = templateService.upload(file, businessId);

        return ApiResponse.ofSuccess(upload);
    }

    @ApiOperation(value = "获取业务下的模板", notes = "获取业务下的模板", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "businessId", value = "业务分类id", required = true, paramType = "path")
    })
    @GetMapping(value = "{businessId}/templates")
    public ApiResponse list(@PathVariable String businessId) {
        List<Template> templates = templateService.list(businessId);
        return ApiResponse.ofSuccess(templates);
    }

    @ApiOperation(value = "保存模板信息", notes = "保存模板信息", httpMethod = "POST")
    @PostMapping(value = "templates")
    @SystemLog(description = "保存模板信息")
    public ApiResponse save( @RequestBody @Validated TemplateDTO templateDTO) {
        Template save = templateService.save( templateDTO);
        return ApiResponse.ofSuccess(save);
    }


    @ApiOperation(value = "修改模板信息", notes = "修改模板信息", httpMethod = "PUT")
    @PutMapping(value = "templates/{id}")
    @SystemLog(description = "修改模板信息")
    public ApiResponse update(@PathVariable String id, @RequestBody TemplateDTO templateDTO) {

        Template update = templateService.update(id, templateDTO);
        return ApiResponse.ofSuccess(update);
    }

    @ApiOperation(value = "模板列表", notes = "模板列表", httpMethod = "GET")
    @GetMapping(value = "templates")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "search", value = "关键词" , paramType = "query"),
            @ApiImplicitParam(name = "type", value = "word excel", paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "下发开始时间", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "下发结束时间", paramType = "query"),
            @ApiImplicitParam(name = "businessId", value = "业务id", paramType = "query")
    })
    @SystemLog(description = "查看模板列表")
    public ApiResponse<PageVO<Template>> page(PageBaseDTO pageBaseDTO, SortDTO sortDTO, String type, String businessId) {

        PageImpl page = templateService.page(pageBaseDTO, sortDTO, type, businessId);

        return ResultUtil.successPagesVO(page);

    }


    @ApiOperation(value = "删除模板", notes = "删除模板", httpMethod = "DELETE")
    @DeleteMapping(value = "templates/{id}")
    @SystemLog(description = "删除模板")
    public ApiResponse del(@PathVariable String id) {
        Boolean del = templateService.del(id);
        return ApiResponse.ofSuccess(del);
    }

    @ApiOperation(value = "模板信息", notes = "模板信息", httpMethod = "GET")
    @GetMapping(value = "templates/{id}")
    public ApiResponse get(@PathVariable String id) {
        Template template = templateService.get(id);
        return ApiResponse.ofSuccess(template);
    }

}
